package org.aiforum.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.aiforum.domain.User;
import org.springframework.stereotype.Repository;

/**
 * 用户持久化实现
 * @author JAVA
 *
 */
@Repository
public class UserDaoImpl  implements UserDao{
	/**
	 * 使用@PersistenceContext注解标注属性时,Spring会自动创建并注入实体管理器
	 */
	@PersistenceContext
	private EntityManager entityManager;

	public void persist(User entity) {
		entityManager.persist(entity);
	}

	public User find(int id) {
	   return entityManager.find(User.class, id);
	}

	public User queryByAccount(String account) {
		Query query = entityManager.createNamedQuery("queryByAccounts");
		query.setParameter("account", account);
		@SuppressWarnings("unchecked")
		List<User> users = query.getResultList();
		if(users.size()>0){
			return users.get(0);
		}
		return null;
	}

	}


